package com.orion.config;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * 参考 UsernamePasswordAuthenticationToken ，它是默认的用户名密码登录的token
 *
 * @author Administrator
 * @date 2021/9/13
 */
public class VerifyCodeAuthenticationToken extends AbstractAuthenticationToken {

    private String verifyCode;

    public VerifyCodeAuthenticationToken(String verifyCode) {
        super(null);
        this.verifyCode = verifyCode;
        this.setAuthenticated(false);
    }

    public VerifyCodeAuthenticationToken(String verifyCode, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.verifyCode = verifyCode;
        super.setAuthenticated(true);
    }


    public String getVerifyCode() {
        return verifyCode;
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return null;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
